package com.gitee.dolt.mail;


import cn.hutool.json.JSONUtil;
import com.gitee.dolt.mail.event.MailSendLogListener;
import com.gitee.dolt.mail.properties.MailProperties;
import com.gitee.dolt.mail.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 邮件自动配置
 *
 * @author LYH
 */
@Configuration
@EnableConfigurationProperties(MailProperties.class)
@Slf4j
public class MailAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MailSendLogListener mailSendLogListener() {
        return new MailSendLogListener((optLogDTO) -> {
            log.debug("{}|{}", "MAIL_SEND_LOG", JSONUtil.toJsonStr(optLogDTO));
        });
    }

    @Bean
    public MailService mailService(MailProperties mailProperties) {
        return new MailService(mailProperties);
    }
}
